var _inherit = require('core/inherit.js');

var _geometry = require('core/glm.js');
var _glm = _geometry.glm;
var _v2 = _geometry.vec2f;
var _v3 = _geometry.vec3f;
var _Sprite = require('drawable/spritenode.js');

/**
 * @param material
 * @param frame
 * @constructor
 */
function PartialSprite(material, frame) {
    _Sprite.call(this, material, frame);

    this.mStart = new _v2();
    this.mRectSize = new _v2();
    this._t = new _v2();
    this._v = new _v3();
}
_inherit(PartialSprite, _Sprite);
PartialSprite.prototype.getRectSize = function () {
    return this.mRectSize;
};
PartialSprite.prototype.getRectPos = function () {
    return this.mStart;
};
PartialSprite.prototype.initPoint = function (f, m, v, t, x, y, accp, acct, i) {
    f.getPointAbs(v, t, x, y);
    _glm.mulMV3(v, m, v);

    acct.set(t);
    accp.set(v);
    this.mBuffer.push(i);
};
PartialSprite.prototype.setRect = function (x, y, width, height) {
    if (arguments.length == 2) {
        width = x;
        height = y;
        x = y = 0;
    }

    var t = this._t;
    var v = this._v;
    this.mStart.set(x, y);
    this.mRectSize.set(width, height);

    var b = this.mBuffer;
    var accp = b.accessor('p');
    var acct = b.accessor('t');
    var f = this.mFrame;
    var m = f.getMatrix();

    this.initPoint(f, m, v, t, x, y, accp, acct, 0);
    this.initPoint(f, m, v, t, x, y + height, accp, acct, 1);
    this.initPoint(f, m, v, t, x + width, y, accp, acct, 2);
    this.initPoint(f, m, v, t, x + width, y + height, accp, acct, 3);
    b.upload();
};

module.exports = PartialSprite;